<template>
  <div>
    <!-- 搜索框 -->
    <el-row id="" style="padding: 10px 0">
      <el-col :span="18">
        <el-input v-model="names" placeholder="用户昵称/用户名"></el-input>
      </el-col>
      <el-col :span="6" style="padding-left: 15px">
        <el-button type="primary" @click="getuserlist(1)">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-col>
    </el-row>
    <!-- 功能按钮 -->
    <el-button
      type="primary"
      v-for="i in $store.state.user.menulist[this.$route.path]"
      :key="i.id"
      @click="btn(i.code)"
      >{{ i.name }}</el-button
    >
    <!--  {{ this.$store.state.user.menulist[this.$route.path] }} -->
    <!-- table表格 -->
    <div v-if="tableData" style="padding: 20px 0">
      <el-table
        :data="tableData"
        style="width: 100%"
        height="300"
        @cell-click="celltable"
        :highlight-current-row="true"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="昵称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="账号">
                <span>{{ props.row.account }}</span>
              </el-form-item>
              <el-form-item label="生日">
                <span>{{ props.row.birthday }}</span>
              </el-form-item>
              <el-form-item label="状态">
                <span>{{ props.row.statusName }}</span>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ props.row.createTime }}</span>
              </el-form-item>
              <el-form-item label="性别">
                <span>{{ props.row.sexName }}</span>
              </el-form-item>
              <el-form-item label="部门">
                <span>{{ props.row.deptName }}</span>
              </el-form-item>
              <el-form-item label="手机号">
                <span>{{ props.row.phone }}</span>
              </el-form-item>
              <el-form-item label="邮箱">
                <span>{{ props.row.email }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="昵称" prop="name"> </el-table-column>
        <el-table-column label="账号" prop="account"> </el-table-column>
        <el-table-column label="状态" prop="statusName"> </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination
        :background="true"
        layout="prev, pager, next"
        :total="total"
        :page-size="5"
        @current-change="pagetype"
      >
      </el-pagination>
    </div>
    <!-- 功能组件 -->
    <!-- 添加用户组件 -->
    <mgr-add
      :type="tabletype.mgrAdd"
      :fun="cleartype"
      :tname="'mgrAdd'"
    ></mgr-add>
    <!-- 修改用户组件 -->
    <mgr-edit
      :type="tabletype.mgrEdit"
      :fun="cleartype"
      :tname="'mgrEdit'"
      :tablerow="tablerow"
    ></mgr-edit>
    <!-- 删除用户组件 -->
    <mgr-delete
      :type="tabletype.mgrDelete"
      :fun="cleartype"
      :tname="'mgrDelete'"
      :tablerow="tablerow"
    ></mgr-delete>
    <!-- 角色分配组件 -->
    <mgr-setrole
      :type="tabletype.mgrSetRole"
      :fun="cleartype"
      :tname="'mgrSetRole'"
      :tablerow="tablerow"
    ></mgr-setrole>
  </div>
</template>

<script>
// 引入网络请求的组价
import { http, userlist } from "../../api/api";
// 引入添加功能的组件
import MgrAdd from "./MgrAdd"; // 添加用户
import MgrEdit from "./MgrEdit"; // 修改用户
import MgrDelete from "./MgrDelete"; //删除用户
import MgrSetrole from "./MgrSetrole"; //角色分配
export default {
  data() {
    return {
      tableData: "", //用户列表数据
      total: Number(), //总数量
      names: "", //搜索的名字
      tabletype: {
        //功能对话框状态
        mgrAdd: false, //添加用户
        mgrEdit: false, //修改用户
        mgrDelete: false, //删除用户
        mgrSetRole: false, //分配角色
      },
      // 单元格选择的对象
      tablerow: "",
      // 记录当前页码
      page: 1,
    };
  },
  components: {
    MgrAdd,
    MgrEdit,
    MgrDelete,
    MgrSetrole,
  },
  mounted() {
    var json = this.$store.state.user.menulist[this.$route.path],
      arr1 = json.slice(0, 3),
      arr2 = json.slice(-1);
    var test = arr1.concat(arr2);
    this.$store.state.user.menulist[this.$route.path] = test;

    this.getuserlist(1);
  },
  methods: {
    // 将tabletype中的地址值变成false状态，关闭模态框
    cleartype(type) {
      this.tabletype[type] = false;
      this.getuserlist(this.page);
      this.tablerow = "";
    },
    // 选择单元格触发的事件
    celltable(row) {
      this.tablerow = row;
      console.log(row);
    },
    // 统一的功能按钮
    btn(a) {
      if (a == "mgrAdd") {
        this.tabletype[a] = true;
      } else {
        if (this.tablerow == "") {
          this.$message.error("请选择要操作的用户");
        } else {
          if (a == "mgrFreeze") {
            if (this.tablerow.statusName == "冻结") {
              this.$message.error("该用户账号已经冻结");
            } else {
              this.tabletype[a] = true;
            }
          } else if (a == "mgrUnfreeze") {
            if (this.tablerow.statusName == "启用") {
              this.$message.error("该用户账号已经启用");
            } else {
              this.tabletype[a] = true;
            }
          } else {
            this.tabletype[a] = true;
          }
        }
      }
    },
    // 重置功能
    reset() {
      this.names = "";
      this.getuserlist(this.page);
    },
    // 点击页码触发的事件
    pagetype(page) {
      this.page = page;
      this.getuserlist(page);
    },
    // 网络请求列表数量
    getuserlist(page) {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        background: "rgba(0, 0, 0, 0.7)",
      });

      this.$http
        .get(http + userlist, {
          params: {
            page: page,
            limit: 5,
            name: this.names,
          },
        })
        .then(
          (data) => {
            console.log(data.data.msg);
            if (data.data.msg == "成功") {
              // 得到的用户的表格信息
              this.tableData = data.data.data.records;
              // 总数
              this.total = data.data.data.total;

              setTimeout(() => {
                loading.close();
              }, 1000);
            } else {
              this.$message.error(data.data.message);
            }
          },
          (err) => {
            this.$message.error(err.data.message);
          }
        );
    },
  },
};
</script>

<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>